package org.luxor.storage.utils;

import org.luxor.commons.core.utils.StrKit;
import org.luxor.commons.storage.exception.StorageServiceException;
import org.luxor.storage.constant.OssConstant;

import java.net.FileNameMap;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.regex.Pattern;

/**
 * 文件参数提取工具
 *
 * @author Mr.yan @date 2020/3/16
 */
public class ParamExtUtils {
    private static Pattern STORAGE_TYPE_PATTERN = Pattern.compile(".*(_[1|2|3|4|5]_)\\d{9}\\.\\w{1,5}");

    public static void main(String[] args) {
        String TEML = "/images/2020-03-23/background_3_000930920.jpg";
        System.out.println(ParamExtUtils.extStorageType(TEML));

        String fileFullName = ParamExtUtils.extFileFullName(TEML);
        String mimeType = getMimeType(fileFullName);

        System.out.println("Content-Disposition: attachment; filename=\"" + fileFullName + "\"");
        System.out.println("Content-Type: " + mimeType);

        String str0 = "https%3A%2F%2Fwww.baidu.com%2Fsso%2Fimages%2F2020-03-23%2Fbackground_0_000939920.jpg%3FstorageType%3D1";
        String str1 = "https://www.baidu.com/sso//images/2020-03-23/background_0_000939920.jpg?storageType=1";
        String str2 = "https://www.baidu.com/sso/images/2020-03-23/background_0_000939920.jpg?storageType=1";
        String str3 = "https://www.baidu.com/sso/images/2020-03-23/background_0_000939920.jpg";
        String str4 = "http://www.baidu.com/sso/images/2020-03-23/background_0_000939920.jpg";
        String str5 = "http://127.0.0.1:8080/sso/images/2020-03-23/background_0_000939920";

        System.out.println(ParamExtUtils.extFileFullPath(str0));
        System.out.println(ParamExtUtils.extFileFullPath(str1));
        System.out.println(ParamExtUtils.extFileFullPath(str2));
        System.out.println(ParamExtUtils.extFileFullPath(str3));
        System.out.println(ParamExtUtils.extFileFullPath(str4));
        System.out.println(ParamExtUtils.extFileFullPath(str5));
    }

    /**
     * 提取URL地址中的fileFullPath参数
     *
     * @param url https://www.baidu.com/sso/images/2020-03-23/background_0_000939920.jpg
     * @return 文件存储目录 /images/2020-03-23/background_0_000939920.jpg
     */
    public static String extFileFullPath(String url) {
        String fileFullPath = null;
        try {
            //如果需要URLDecoder解码
            if (url.indexOf(StrKit.SLASH_URL) != -1) {
                url = URLDecoder.decode(url, StrKit.CHARSET_NAME);
            }
            int beginIndex = url.indexOf(OssConstant.SSO_PATH_MARK) + OssConstant.SSO_PATH_MARK.length();
            int endIndex = url.indexOf(StrKit.QUESTION);
            if (endIndex == -1) {
                endIndex = url.length();
            }
            fileFullPath = url.substring(beginIndex, endIndex);
            return fileFullPath.replaceAll(StrKit.SLASH_DUAL, StrKit.SLASH);
        } catch (Throwable e) {
            throw new StorageServiceException("OSS文件路径不合法，解析FileFullPath参数失败", e);
        }
    }

    /**
     * 提取文件存储的StorageType类型参数
     *
     * @param fileFullPath /images/2020-03-23/background_1_000939920.jpg
     * @return 1
     */
    public static Integer extStorageType(String fileFullPath) {
        try {
            if (Pattern.matches(STORAGE_TYPE_PATTERN.pattern(),fileFullPath)) {
                int storageTypeIndex = fileFullPath.lastIndexOf(StrKit.DOT) - 11;
                if (storageTypeIndex >= 0) {
                    String storageType = String.valueOf(fileFullPath.charAt(storageTypeIndex));
                    return Integer.parseInt(storageType);
                }
            }
            return 1;
        } catch (Throwable e) {
            throw new StorageServiceException("OSS文件路径不合法，解析storageType参数失败", e);
        }
    }


    /**
     * 提取文件全名称
     *
     * @param fileFullPath /images/2020-03-23/background_1_000939920.jpg
     * @return background_1_000939920.jpg
     */
    public static String extFileFullName(String fileFullPath) {
        try {
            int beginIndex = fileFullPath.lastIndexOf(StrKit.SLASH) + 1;
            int endIndex = fileFullPath.length();
            String fileName = fileFullPath.substring(beginIndex, endIndex);
            return fileName;
        } catch (Throwable e) {
            throw new StorageServiceException("OSS文件路径不合法，解析storageType参数失败", e);
        }
    }

    /**
     * 提取文件名称
     *
     * @param fileFullPath /images/2020-03-23/background_1_000939920.jpg
     * @return background_1_000939920
     */
    public static String extFileName(String fileFullPath) {
        try {
            int beginIndex = fileFullPath.lastIndexOf(StrKit.SLASH) + 1;
            int endIndex = fileFullPath.lastIndexOf(StrKit.DOT);
            if (endIndex == -1) {
                endIndex = fileFullPath.length();
            }
            String fileName = fileFullPath.substring(beginIndex, endIndex);
            return fileName;
        } catch (Throwable e) {
            throw new StorageServiceException("OSS文件路径不合法，解析storageType参数失败", e);
        }
    }

    /**
     * 提取文件类型后缀
     *
     * @param fileFullPath /images/2020-03-23/background_1_000939920.jpg
     * @return .jpg
     */
    public static String extFilePostfix(String fileFullPath) {
        try {
            int beginIndex = fileFullPath.lastIndexOf(StrKit.DOT);
            int endIndex = fileFullPath.length();
            String filePostfix = fileFullPath.substring(beginIndex, endIndex);
            return filePostfix;
        } catch (Throwable e) {
            throw new StorageServiceException("OSS文件路径不合法，解析文件后缀类型失败", e);
        }
    }

    /**
     * 根据文件名提取文件的MimeType类型
     * @param filePath
     * @return
     */
    public static String getMimeType(String filePath) {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String type = fileNameMap.getContentTypeFor(filePath);
        return type;
    }
}
